<template>
  <div class="user-articles">
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="全部文章" name="all"></el-tab-pane>
      <el-tab-pane label="热门文章" name="hot"></el-tab-pane>
      <el-tab-pane
        v-for="category in categories"
        :key="category.categoryId"
        :label="category.categoryName"
        :name="category.categoryId.toString()"
      ></el-tab-pane>
    </el-tabs>
    <el-table :data="articles" border>
      <el-table-column prop="title" label="标题" width="180"></el-table-column>
      <el-table-column
        prop="categoryName"
        label="类别"
        width="180"
      ></el-table-column>
      <el-table-column label="阅读量" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.readCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            @click="readArticle(scope.row.articleId)"
            type="text"
            size="small"
            >阅读</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: "all",
      articles: [],
      categories: [],
      currentPage: 1,
      pageSize: 15,
      total: 0,
    };
  },
  created() {
    this.fetchCategories();
    this.fetchArticles();
  },
  methods: {
    fetchCategories() {
      this.$axios
        .get("/api/article/category/list")
        .then((res) => {
          if (res.data.code === "001") {
            this.categories = res.data.data;
          } else {
            this.notifyError(res.data.msg);
          }
        })
        .catch(() => {
          this.notifyError("请求失败");
        });
    },
    fetchArticles() {
      let url = "/api/article/list";
      let params = { currentPage: this.currentPage, pageSize: this.pageSize };
      if (this.activeTab === "hot") {
        url = "/api/article/hots";
        params = {};
      } else if (this.activeTab !== "all") {
        url = "/api/article/bycategory";
        params.categoryId = this.activeTab;
      }
      this.$axios
        .get(url, { params })
        .then((res) => {
          if (res.data.code === "001") {
            this.articles = res.data.data.list || res.data.data;
            // 确保总数从正确的位置获取
            this.total = res.data.total; // 修改此处
          } else {
            this.notifyError(res.data.msg);
          }
        })
        .catch(() => {
          this.notifyError("请求失败");
        });
    },
    handleTabClick() {
      this.currentPage = 1;
      this.fetchArticles();
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.fetchArticles();
    },
    readArticle(articleId) {
      this.$router.push({ path: "/article/detail", query: { articleId } });
    },
    notifyError(msg) {
      this.$notify.error({
        title: "错误",
        message: msg,
      });
    },
  },
};
</script>

<style scoped>
.user-articles {
  padding: 20px;
}
.user-articles .el-tabs {
  margin-bottom: 20px;
}
.user-articles .el-table {
  margin-bottom: 20px;
}
</style>
